package com.deng.common.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.common.dao.IAdminDepartment;
import com.deng.common.vo.AdminDepartmentVo;
import com.deng.common.vo.OrganizationVo;
import com.deng.util.FunctionFilter;

public class AdminDepartmentImpl extends SQLHelper implements IAdminDepartment {

	public List<AdminDepartmentVo> addAndGetAdminDepartmentList(AdminDepartmentVo vo) {
		try{
			Connection con = getConnect();
			ResultSet rs = queryData(con,"select case when max(id)+1 is null then 1 else max(id)+1 end as id" +
					" from t_sys_stat_admin_department");	
			int maxID = 0;
			if(rs.next()){
				maxID = rs.getInt("id");
			}
			
			String sql ="insert into t_sys_stat_admin_department(id,name,code)" +
					"values(?,?,?)";
			try{
				System.out.println(sql);
				PreparedStatement pst=con.prepareStatement(sql);
				pst.setInt(1, maxID);
				pst.setString(2, vo.name);
				pst.setString(3, vo.code);
				pst.executeUpdate();
				pst.close();
				
				return getAdminDepartmentListByCode(null);
			}catch(Exception ex){
				ex.printStackTrace();
				freeConnection(con);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public List<AdminDepartmentVo> deleteAndGetAdminDepartmentList(AdminDepartmentVo vo) {
		try{
			Connection con = getConnect();
						
			String sql ="delete t_sys_stat_admin_department where id = "+vo.id;
			try{
				System.out.println(sql);
				PreparedStatement pst=con.prepareStatement(sql);
				pst.executeUpdate();
				pst.close();
				
				return getAdminDepartmentListByCode(null);
			}catch(Exception ex){
				ex.printStackTrace();
				freeConnection(con);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	public List<AdminDepartmentVo> updateAndGetAdminDepartmentList(
			AdminDepartmentVo vo) {
		try{
			Connection con = getConnect();
						
			String sql ="update t_sys_stat_admin_department set name = ? ,code = ? where id = "+vo.id;
			try{
				System.out.println(sql);
				PreparedStatement pst=con.prepareStatement(sql);
				pst.setString(1, vo.name);
				pst.setString(2, vo.code);
				pst.executeUpdate();
				pst.close();
				
				return getAdminDepartmentListByCode(null);
			}catch(Exception ex){
				ex.printStackTrace();
				freeConnection(con);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	public List<AdminDepartmentVo> getAdminDepartmentListByCode(String code) {
		try{
			Connection conn = getConnect();
			
			String sql ;
			if(code == null){
				sql = "select * from t_sys_stat_admin_department order by name";
			}else{
				sql = "select * from t_sys_stat_admin_department where code = '"+code+"' order by name";
			}
			ResultSet rs = queryData(conn,sql);	
			List<AdminDepartmentVo> list = new ArrayList<AdminDepartmentVo>();
			while(rs.next()){
				AdminDepartmentVo vo = new AdminDepartmentVo();
				vo.id = rs.getInt("id");
				vo.name = rs.getString("name");
				vo.code = rs.getString("code");
				list.add(vo);
			}
			freeConnection(conn);
			return list;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}




}
